import urllib2,sys
import urllib,sys
import re
	
def generalSearch(facultyName, schoolName):
	url = "http://academic.research.microsoft.com/Search?query=org%3a%28"+urllib.quote(schoolName)+"%29%20author%3a%28"+urllib.quote(facultyName)+"%29&SearchDomain=2"
	
	return urllib2.urlopen(url).read()
	
def getSuggestedAuthorId(html):
	# use regex, possibly a better way to do it, but this works for now
	m = re.search("<li(.*)><a\shref=\"([^\"]*)\">([^<]*<b>[^<]*<\/b>[^<]*)+<\/a>", html)
	
	id = m.group(0).split("<a href=\"/Author/")[1].split("/")[0]
	
	return id


def getAuthorPage(id):
	url = "http://academic.research.microsoft.com/Author/"+str(id)
	
	return urllib2.urlopen(url).read()
	
def getFullPage(facultyName, schoolName, restrictBySchool):
	if(restrictBySchool == True):
		url = "http://interaction.lille.inria.fr/~roussel/projects/scholarindex/index.cgi?btnG=Search+Scholar&as_sauthors="+urllib.quote(facultyName)+"&as_allsubj=all&as_q="+urllib.quote(schoolName)+"&as_oq=&as_eq=&as_publication=&as_ylo=&as_yhi=&x_minr=&as_occt=any&hl=en&lr="
	else:
		url = "http://interaction.lille.inria.fr/~roussel/projects/scholarindex/index.cgi?btnG=Search+Scholar&as_sauthors="+urllib.quote(facultyName)+"&as_allsubj=all&as_q=&as_oq=&as_eq=&as_publication=&as_ylo=&as_yhi=&x_minr=&as_occt=any&hl=en&lr=";

	return urllib2.urlopen(url).read()